package com.example.jf.jfapp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jf.jfapp.R;
import com.example.jf.jfapp.bean.request.NearUserListRequest;
import com.example.jf.jfapp.utils.AppConfigUtils;

import java.util.ArrayList;

import loopj.android.image.SmartImageView;

/**
 * Created by jf on 2017/8/1.
 */

public class NearUserListAdapter extends RecyclerView.Adapter {
    private static final String TAG = "NearUserListAdapter";
    ArrayList<NearUserListRequest>nlists;
    boolean isfocus=false;
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_near_user_list,parent,false);
       itemViewHolder item=new itemViewHolder(view);
        return item;
    }

    public NearUserListAdapter(ArrayList<NearUserListRequest> nlists) {
        this.nlists = nlists;
    }

    class  itemViewHolder extends RecyclerView.ViewHolder{
        SmartImageView imageView;
        TextView username;
        TextView sign;
        TextView btn;
        public itemViewHolder(View itemView) {
            super(itemView);
            imageView=(SmartImageView)itemView.findViewById(R.id.iv_focus);
            username=(TextView)itemView.findViewById(R.id.tv_name);
            sign=(TextView)itemView.findViewById(R.id.sign);
            btn=(TextView) itemView.findViewById(R.id.tv_focus);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final itemViewHolder itemViewHolder=(NearUserListAdapter.itemViewHolder)holder;
        final NearUserListRequest rr=nlists.get(position);
        itemViewHolder.imageView.setImageUrl(rr.getHeadProtrait());
        itemViewHolder.username.setText(rr.getUsername());
        itemViewHolder.sign.setText(rr.getSignature());
//点击关注窗口
        itemViewHolder.btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

            String userId= AppConfigUtils.getInstance().getLoginInfo(itemViewHolder.itemView.getContext()).getObjectId();
            String otherId=rr.getUserId();
        if (!isfocus){
            itemViewHolder.btn.setText("取消关注");
            Toast.makeText(itemViewHolder.itemView.getContext(),"关注成功",Toast.LENGTH_SHORT).show();
            isfocus=true;
        }
        else{
            itemViewHolder.btn.setText("关注");
            Toast.makeText(itemViewHolder.itemView.getContext(),"取消成功",Toast.LENGTH_SHORT).show();
            isfocus=false;
        }

//            focus(userId,otherId);
    }
});
    }
//    private void  focus(String userId,String otherId){
//
//        OkHttpClient okHttpClient = new OkHttpClient();
//        Request request=new Request.Builder()
//                .addHeader("X-LC-Id", "S5KdTtqGDUwpImMvQ6HAJYaL-gzGzoHsz")
//                .addHeader("X-LC-Key", "NRPgCGiGxjbtGN2Bn0mlglrL")
//                .addHeader("Content-Type", "application/json")
//                .url("https://api.leancloud.cn/1.1/users/" +userId + "/friendship/" + otherId)
//                .post(body)
//                .build();
//
//        okHttpClient.newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                e.printStackTrace();
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//            String result=response.body().string();
//                Log.e(TAG, "onResponse: "+result );
//            }
//        });
//    }

    @Override
    public int getItemCount() {
        return nlists.size();
    }
}
